﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class Modules_DetailSportOddsResult : WebControlBase
{
    private long ServiceDetailID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceDetailID = Request.QueryString["servDetailId"] != null ? Int32.Parse(Request.QueryString["servDetailId"].ToString().Trim()) : -1;

        if (!this.IsPostBack)
        {
            this.txtContent1.Attributes.Add("onkeyup", "javascript:validLen(this,160,content1);");
            this.txtContent1.Attributes.Add("onfocus", "javascript:validLen(this,160,content1);");
            this.txtContent1.Attributes.Add("onchange", "javascript:validLen(this,160,content1);");

            this.txtContent2.Attributes.Add("onkeyup", "javascript:validLen(this,160,content2);");
            this.txtContent2.Attributes.Add("onfocus", "javascript:validLen(this,160,content2);");
            this.txtContent2.Attributes.Add("onchange", "javascript:validLen(this,160,content2);");

            this.txtContent3.Attributes.Add("onkeyup", "javascript:validLen(this,160,content3);");
            this.txtContent3.Attributes.Add("onfocus", "javascript:validLen(this,160,content3);");
            this.txtContent3.Attributes.Add("onchange", "javascript:validLen(this,160,content3);");

            this.txtContent4.Attributes.Add("onkeyup", "javascript:validLen(this,160,content4);");
            this.txtContent4.Attributes.Add("onfocus", "javascript:validLen(this,160,content4);");
            this.txtContent4.Attributes.Add("onchange", "javascript:validLen(this,160,content4);");

            this.txtContent5.Attributes.Add("onkeyup", "javascript:validLen(this,160,content5);");
            this.txtContent5.Attributes.Add("onfocus", "javascript:validLen(this,160,content5);");
            this.txtContent5.Attributes.Add("onchange", "javascript:validLen(this,160,content5);");
        }

        if (ServiceDetailID == -1 && ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1 && ClientState.MemberInfo.UserType != 2)
        {
            this.btnAdd.Enabled = false;
            this.btnSaveTop.Enabled = false;
            this.lblError.Text = "Bạn không có quyền tạo mã!";
        }

        if (this.IsPostBack) return;

        this.txtContent4.Enabled = false;
        this.txtContent5.Enabled = false;

        this.GetService();
        this.BindGroup();

        if (ServiceDetailID > 0)
        {
            this.BindObject();
        }
    }

    private void BindGroup()
    {
        this.ddlGroup.DataSource = CodeManager.GetCodeDetailTable("SERVICE", "SERVICE_GROUP");
        this.ddlGroup.DataBind();
        this.ddlGroup.Items.Insert(0, new ListItem("-- None --", "0"));
    }

    private void GetService()
    {
        if (ClientState.MemberInfo.VerifyCode != "")
        {
            DataTable dt = ServicesManager.ListByRange(ClientState.MemberInfo.VerifyCode);
            if (dt != null && dt.Rows.Count > 0)
            {
                this.ddlService.DataSource = dt;
                this.ddlService.DataBind();
            }
            else
            {
                this.ddlService.DataSource = null;
                this.ddlService.DataBind();
            }
        }
    }

    private void BindObject()
    {
        if (ServiceDetailID != -1)
        {
            ServiceDetailsInfo objService = ServiceDetailsManager.Select(ServiceDetailID);
            if (objService != null)
            {
                if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.VerifyCode.IndexOf(objService.Cmd1.ToLower()) <= 0)
                {
                    btnAdd.Enabled = false;
                    btnSaveTop.Enabled = false;
                }

                this.ddlService.Text = objService.Cmd1;
                this.txtCmd2.Text = objService.Cmd2;
                this.ddlGroup.SelectedValue = objService.GroupCD.ToString();

                this.txtParam1.Text = objService.Param1;
                this.txtParam2.Text = objService.Param2;
                this.txtParam3.Text = objService.Param3;
                this.txtParam4.Text = objService.Param4;
                this.txtParam5.Text = objService.Param5;
                this.txtParam6.Text = objService.Param6;

                this.txtContent1.Text = objService.Content1;
                this.txtContent2.Text = objService.Content2;
                this.txtContent3.Text = objService.Content3;
                this.txtContent4.Text = objService.Content4;
                this.txtContent5.Text = objService.Content5;

                string[] sTime1 = string.IsNullOrEmpty(objService.Timer1) ? new string[0] : objService.Timer1.Split('^');
                if (sTime1.Length < 4)
                {
                    this.txtStartTime1.Text = "";
                    this.txtHTime1.Text = "";
                    this.chkFT1.Checked = false;
                    this.chkEnable1.Checked = false;
                }
                else
                {
                    this.txtStartTime1.Text = sTime1[0];
                    this.txtHTime1.Text = sTime1[1];
                    this.chkEnable1.Checked = sTime1[2] == "1";
                    this.chkFT1.Checked = sTime1[3] == "1";
                }

                string[] sTime2 = string.IsNullOrEmpty(objService.Timer2) ? new string[0] : objService.Timer2.Split('^');
                if (sTime2.Length < 4)
                {   
                    this.txtStartTime2.Text = "";
                    this.txtHTime2.Text = "";
                    this.chkFT2.Checked = false;
                    this.chkEnable2.Checked = false;
                }
                else
                {
                    this.txtStartTime2.Text = sTime2[0];
                    this.txtHTime2.Text = sTime2[1];
                    this.chkEnable2.Checked = sTime2[2] == "1";
                    this.chkFT2.Checked = sTime2[3] == "1";
                }

                string[] sTime3 = string.IsNullOrEmpty(objService.Timer3) ? new string[0] : objService.Timer3.Split('^');
                if (sTime3.Length < 4)
                {
                    this.txtStartTime3.Text = "";
                    this.txtHTime3.Text = "";
                    this.chkFT3.Checked = false;
                    this.chkEnable3.Checked = false;                    
                }
                else
                {
                    this.txtStartTime3.Text = sTime3[0];
                    this.txtHTime3.Text = sTime3[1];
                    this.chkEnable3.Checked = sTime3[2] == "1";
                    this.chkFT3.Checked = sTime3[3] == "1";
                }

                this.ddlContentType.SelectedValue = objService.ContentType.ToString();
                this.ddlMsgType.SelectedValue = objService.MsgType.ToString();

                this.txtTotalMsg.Text = objService.NumOfMsg.ToString();

                this.chkStatus.Checked = objService.Status == 1 ? true : false;
            }
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?module=ListServiceDetail");
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            ActivitiesInfo objActive = new ActivitiesInfo();

            if (ServiceDetailID != -1)
            {
                ServiceDetailsInfo objService = ServiceDetailsManager.Select(ServiceDetailID);
                if (objService != null)
                {
                    objService.Cmd1 = this.ddlService.SelectedValue;
                    objService.Cmd2 = this.txtCmd2.Text;

                    objService.Param1 = this.txtParam1.Text;
                    objService.Param2 = this.txtParam2.Text;
                    objService.Param3 = this.txtParam3.Text;
                    objService.Param4 = this.txtParam4.Text;
                    objService.Param5 = this.txtParam5.Text;
                    objService.Param6 = this.txtParam6.Text;

                    if (this.txtParam1.Text.Trim() != "")
                        objService.Param3 = objService.Cmd1 + "_" + this.txtParam1.Text.Trim();

                    string[] sContents = new string[5];
                    int iCont = 0;
                    for (int i = 1; i < 6; i++)
                    {
                        TextBox txt = this.FindControl("txtContent" + i.ToString()) as TextBox;
                        if (txt != null && !txt.Text.Replace(" ", "").Equals(""))
                        {
                            sContents[iCont] = txt.Text;
                            iCont++;

                            //Add to log
                            objActive.DoContent = objActive.DoContent + "/" + txt.Text;
                            objActive.TableName = String.Format("{0}_{1}_{2}", objService.Cmd1, objService.Code1, objService.Param1);
                        }
                    }

                    objService.Content1 = sContents[0];
                    objService.Content2 = sContents[1];
                    objService.Content3 = sContents[2];
                    objService.Content4 = sContents[3];
                    objService.Content5 = sContents[4];

                    objService.GroupCD = int.Parse(this.ddlGroup.SelectedValue);
                    objService.ContentType = byte.Parse(this.ddlContentType.SelectedValue);
                    objService.MsgType = byte.Parse(this.ddlMsgType.SelectedValue);
                    objService.ProcessType = byte.Parse(this.ddlProcessType.SelectedValue);
                    objService.CreatedBy = ClientState.MemberInfo.Username;
                    objService.CreatedDate = DateTime.Now;

                    byte j = 0;

                    if (this.txtContent1.Text.Replace(" ", "") != "")
                        j++;
                    if (this.txtContent2.Text.Replace(" ", "") != "")
                        j++;
                    if (this.txtContent3.Text.Replace(" ", "") != "")
                        j++;
                    if (this.txtContent4.Text.Replace(" ", "") != "")
                        j++;
                    if (this.txtContent5.Text.Replace(" ", "") != "")
                        j++;

                    objService.NumOfMsg = j;

                    string[] sTime1 = new string[] { this.txtStartTime1.Text, this.txtHTime1.Text, this.chkEnable1.Checked ? "1" : "0", this.chkFT1.Checked ? "1" : "0" };
                    string[] sTime2 = new string[] { this.txtStartTime2.Text, this.txtHTime2.Text, this.chkEnable2.Checked ? "1" : "0", this.chkFT2.Checked ? "1" : "0" };
                    string[] sTime3 = new string[] { this.txtStartTime3.Text, this.txtHTime3.Text, this.chkEnable3.Checked ? "1" : "0", this.chkFT3.Checked ? "1" : "0" };

                    objService.Timer1 = string.Join("^", sTime1);
                    objService.Timer2 = string.Join("^", sTime2);
                    objService.Timer3 = string.Join("^", sTime3);

                    if (this.chkStatus.Checked)
                        objService.Status = 1;
                    else
                        objService.Status = 0;

                    bool result = ServiceDetailsManager.Update(objService);
                    if (result)
                    {
                        this.lblError.Text = "Cập nhật thành công!";
                    }
                    else
                        this.lblError.Text = "Cập nhật lỗi!";

                    objActive.Action = "Update";
                }
            }
            else
            {
                ServiceDetailsInfo objService = new ServiceDetailsInfo();

                objService.Cmd1 = this.ddlService.SelectedValue;
                objService.Cmd2 = this.txtCmd2.Text;

                objService.Param1 = this.txtParam1.Text;
                objService.Param2 = this.txtParam2.Text;
                objService.Param3 = this.txtParam3.Text;
                objService.Param4 = this.txtParam4.Text;
                objService.Param5 = this.txtParam5.Text;
                objService.Param6 = this.txtParam6.Text;

                if (this.txtParam1.Text.Trim() != "")
                    objService.Param3 = objService.Cmd1 + "_" + this.txtParam1.Text.Trim();

                string[] sContents = new string[5];
                int iCont = 0;
                for (int i = 1; i < 6; i++)
                {
                    TextBox txt = this.FindControl("txtContent" + i.ToString()) as TextBox;
                    if (txt != null && !txt.Text.Replace(" ", "").Equals(""))
                    {
                        sContents[iCont] = txt.Text;
                        iCont++;

                        //Add to log
                        objActive.DoContent = objActive.DoContent + "/" + txt.Text;
                        objActive.TableName = String.Format("{0}_{1}_{2}", objService.Cmd1, objService.Code1, objService.Param1);
                    }
                }

                objService.Content1 = sContents[0];
                objService.Content2 = sContents[1];
                objService.Content3 = sContents[2];
                objService.Content4 = sContents[3];
                objService.Content5 = sContents[4];

                objService.GroupCD = int.Parse(this.ddlGroup.SelectedValue);
                objService.ContentType = byte.Parse(this.ddlContentType.SelectedValue);
                objService.MsgType = byte.Parse(this.ddlMsgType.SelectedValue);
                objService.ProcessType = byte.Parse(this.ddlProcessType.SelectedValue);
                objService.CreatedDate = DateTime.Now;
                objService.CreatedBy = ClientState.MemberInfo.Username;

                byte j = 0;

                if (this.txtContent1.Text.Replace(" ", "") != "")
                    j++;
                if (this.txtContent2.Text.Replace(" ", "") != "")
                    j++;
                if (this.txtContent3.Text.Replace(" ", "") != "")
                    j++;
                if (this.txtContent4.Text.Replace(" ", "") != "")
                    j++;
                if (this.txtContent5.Text.Replace(" ", "") != "")
                    j++;

                objService.NumOfMsg = j;

				string[] sTime1 = new string[] { this.txtStartTime1.Text, this.txtHTime1.Text, this.chkEnable1.Checked ? "1" : "0", this.chkFT1.Checked ? "1" : "0" };
				string[] sTime2 = new string[] { this.txtStartTime2.Text, this.txtHTime2.Text, this.chkEnable2.Checked ? "1" : "0", this.chkFT2.Checked ? "1" : "0" };
				string[] sTime3 = new string[] { this.txtStartTime3.Text, this.txtHTime3.Text, this.chkEnable3.Checked ? "1" : "0", this.chkFT3.Checked ? "1" : "0" };

				objService.Timer1 = string.Join("^", sTime1);
				objService.Timer2 = string.Join("^", sTime2);
				objService.Timer3 = string.Join("^", sTime3);

                if (this.chkStatus.Checked)
                    objService.Status = 1;
                else
                    objService.Status = 0;

                bool result = ServiceDetailsManager.Insert(objService);
                if (result)
                    Response.Redirect("Default.aspx?module=ListServiceDetail");
                else
                    this.lblError.Text = "Thêm mới lỗi!";

                objActive.Action = "Insert";
            }

            objActive.ActivityDate = DateTime.Now;
            objActive.UserID = ClientState.MemberInfo.MemberID;

            ActivitiesManager.Insert(objActive);
        }
        catch { }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        this.txtCmd2.Text = "";
        this.txtContent1.Text = "";
        this.txtContent2.Text = "";
        this.txtContent3.Text = "";
        this.txtContent4.Text = "";
        this.txtContent5.Text = "";
        this.txtParam1.Text = "";
        this.txtParam2.Text = "";
        this.txtParam3.Text = "";
        this.txtParam4.Text = "";
        this.txtParam5.Text = "";
        this.txtParam6.Text = "";
        this.txtTotalMsg.Text = "1";
    }

    protected void ddlService_SelectedIndexChanged(object sender, EventArgs e)
    {
        string key = this.ddlService.SelectedItem.Text.Substring(this.ddlService.SelectedItem.Text.LastIndexOf("-") + 1).Trim();

        if (key == "8078" || key == "8178")
        {
            this.txtContent2.Enabled = false;
            this.txtContent3.Enabled = false;
            this.txtContent4.Enabled = false;
            this.txtContent5.Enabled = false;
        }
        else if (key == "8278" || key == "8378" || key == "8478" || key == "8578")
        {
            this.txtContent4.Enabled = false;
            this.txtContent5.Enabled = false;
        }
        else
        {
            this.txtContent1.Enabled = true;
            this.txtContent2.Enabled = true;
            this.txtContent3.Enabled = true;
            this.txtContent4.Enabled = true;
            this.txtContent5.Enabled = true;
        }
    }
}
